package com.lihh.rocket5.normal;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

public class TagFilterProducer {
  public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, MQBrokerException, RemotingException, InterruptedException {
    // 拿到 生产者实例
    DefaultMQProducer producer = new DefaultMQProducer("producer_group_002");
    // 设置 name server
    producer.setNamesrvAddr("localhost:9876");
    // producer 实例
    producer.start();
    
    String[] tags = new String[]{"A", "B", "C"};
    for (int i = 0; i < 10; i++) {
      // 设置 message tag
      String currentTag = tags[i % tags.length];
      Message message = new Message("order005", currentTag, String.format("tag filter producer %s", i + 1).getBytes(RemotingHelper.DEFAULT_CHARSET));
      SendResult send = producer.send(message);
      System.out.println(send);
    }
  }
}
